package toy.keli.picmap.model;


import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import toy.keli.picmap.BaseActivity;
import toy.keli.picmap.model.types.PhotoItem;
import toy.keli.picmap.tools.Tools;

//Android 微信分享图片  https://www.jianshu.com/p/ce9f286bdd68，  https://blog.csdn.net/qq_26446715/article/details/90715429
public class Weixin extends Tools {
    BaseActivity act;
    public Weixin(BaseActivity act) {
        this.act=act;
    }

    public void sharePicture(List<PhotoItem> pics, boolean toFriend) {
        if(!act.getPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE,()->{ new Weixin(act).sharePicture(pics,toFriend); })) {
            system.msg(act,"请先授权文件读取");
            return;
        }
        // 图片File
        //分享的图片集合
        ArrayList<Uri> imageUris = new ArrayList<Uri>();
        for (int i = 0; i < pics.size(); i++) {
            //网络图片保存到本地
            String fileName=pics.get(i).getPath();
            File f = new File(fileName);
            imageUris.add(Uri.fromFile(f));
        }
//分享到微信好友
        Intent intent = new Intent();
        ComponentName componentName = toFriend?  new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI") :
                new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");

        if (imageUris.size() == 0) return;

        intent.setComponent(componentName);
        intent.setAction(Intent.ACTION_SEND_MULTIPLE);
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_STREAM, imageUris);
        act.startActivity(intent);
    }

}